package com.itcast.controller;

import com.itcast.anno.Log;
import com.itcast.pojo.Dept;
import com.itcast.pojo.Result;
import com.itcast.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
@Api(tags = "部门管理")
public class DeptController {

    //获取日志记录对象
    //private static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired //注入
    private DeptService deptService;

    /**
     * 部门列表数据查询
     *
     * @return 返回所有部门数据
     */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)  //指定请求方式为get
    @GetMapping
    @ApiOperation(value = "查询全部部门")
    public Result list() {
        //logger.info("查询部门全部数据");
        log.info("查询部门全部数据");

        //调用service查询部门数据
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除部门
     *
     * @return
     */
    @Log
    @ApiOperation(value = "根据id删除部门")
    @DeleteMapping("/{id}")
    public Result delete(@ApiParam(value = "部门id", required = true) @PathVariable Integer id) {
        log.info("根据id删除部门：{}", id);

        //调用service根据id删除部门
        deptService.delete(id);

        return Result.success();
    }

    /**
     * 新增部门
     *
     * @param dept 部门接收JSON数据
     * @return
     */
    @Log
    @PostMapping
    @ApiOperation(value = "新增部门")
    public Result add(@ApiParam(value = "部门", required = true) @RequestBody Dept dept) {

        log.info("新增部门：{}", dept);

        //调用service新增部门
        deptService.add(dept);

        return Result.success();
    }

    /**
     * 根据id查询部门数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门数据")
    public Result getDeptById(@ApiParam(value = "部门id", required = true) @PathVariable Integer id) {

        log.info("根据id查询部门数据：{}", id);

        //调用service根据id查询部门
        Dept dept = deptService.getDeptById(id);

        return Result.success(dept);
    }

    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @Log
    @PutMapping
    @ApiOperation(value = "修改部门")
    public Result update(@ApiParam(value = "部门", required = true) @RequestBody Dept dept) {

        log.info("根据id修改部门：{}", dept);

        //调用service的更新部门
        deptService.updateDeptById(dept);

        return Result.success();
    }

}
